//
//  SoundSetting.m
//  SysMenu
//
//  Created by superking on 10-8-23.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "SoundSetting.h"
#import "FileTools.h"
#import "MenuShezhi.h"


@implementation SoundSetting

+(id) scene
{
	// 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	MenuShezhi *layer = [MenuShezhi node];
	
	// add layer as a child to scene
	[scene addChild: layer];
	
	// return the scene
	return scene;
}

//获取语言文件   1:English 2:简体中文 3:繁体中文
int language=0 ;
-(void)panduan{
	//int language=0 ;
	CReader readFile;
	if(readFile.Open("GameLanguage"))
	{
		readFile.ReadInt(&language);
		readFile.Close();
	}
	else {
		language=1;
	}
}


//文字加载（顶部文字和底部按钮）
-(void)WenZi{		
	
	[self panduan];
	switch (language) {
		case 2:
			wzUp=[CCSprite spriteWithFile:@"Set-music_Word_Cn1.png" rect:CGRectMake(0, 0, 112, 14)];
			wzDown=[CCSprite spriteWithFile:@"Set-music_Word_Cn1.png" rect:CGRectMake(0, 28, 58, 14)];
			break;
		case 3:
			wzUp=[CCSprite spriteWithFile:@"Set-music_Word_Cn2.png" rect:CGRectMake(0, 0, 112, 14)];
			wzDown=[CCSprite spriteWithFile:@"Set-music_Word_Cn2.png" rect:CGRectMake(0, 28, 58, 14)];
			break;
		default:
			wzUp=[CCSprite spriteWithFile:@"Set-music_Word_En.png" rect:CGRectMake(0, 0, 112, 14)];
			wzDown=[CCSprite spriteWithFile:@"Set-music_Word_En.png" rect:CGRectMake(0, 28, 58, 14)];
			break;
	}
	
	wzUp.position=ccp(240,255);
	wzDown.position=ccp(236,24);
	[self addChild:wzUp z:4];
	[self addChild:wzDown z:4];
}


-(id)init{
	self=[super init];
	if (self) {
		self.isTouchEnabled = YES;		
		//背景
		CCSprite *bg=[CCSprite spriteWithFile:@"back_Hiphop.png"];
		bg.position=ccp(240,160);
		[self addChild:bg z:0];
		
		//左右Dj
		CCSprite *bgLDj=[CCSprite spriteWithFile:@"soundfx_DJzuo.png"];
		bgLDj.anchorPoint=ccp(0,0);
		bgLDj.position=ccp(0,25);
		[self addChild:bgLDj z:1];
		
		CCSprite *bgRDj=[CCSprite spriteWithFile:@"soundfx_DJyou.png"];
		bgRDj.anchorPoint=ccp(1,0);
		bgRDj.position=ccp(480,25);
		[self addChild:bgRDj z:1];		
		
		//背景框
		CCSprite *bgUp=[CCSprite spriteWithFile:@"Set-music_Bbox.png"];
		bgUp.position=ccp(240,255);
		[self addChild:bgUp z:2];
		
		CCSprite *black=[CCSprite spriteWithFile:@"black_tiao.png" ];
		black.rotation=180;
		black.position=ccp(240,14);
		[self addChild:black z:2];
		
		bgDown=[CCSprite spriteWithFile:@"Set-music_Mbox.png" rect:CGRectMake(0, 0, 216, 28)];
		bgDown.position=ccp(240,24);
		[self addChild:bgDown z:3];
		
		bgLeft=[CCSprite spriteWithFile:@"Set-music_Sbox.png" rect:CGRectMake(0, 0, 104, 22)];
		bgLeft.anchorPoint=ccp(0,0);
		bgLeft.position=ccp(20,20);
		[self addChild:bgLeft z:3];
		
		bgRight=[CCSprite spriteWithFile:@"Set-music_Sbox.png" rect:CGRectMake(0, 0, 104, 22)];
		bgRight.anchorPoint=ccp(1,0);
		bgRight.position=ccp(460,20);
		[self addChild:bgRight z:3];	
		
		//加载文字（顶部和底部）
		[self WenZi];
		
		//加载左右按钮文字
		switch (language) {
			case 2:
				wzLeft=[CCSprite spriteWithFile:@"Set-music_Word_Cn1.png" rect:CGRectMake(0, 14, 31, 14)];
				wzRight=[CCSprite spriteWithFile:@"Set-music_Word_Cn1.png" rect:CGRectMake(32, 14, 56, 14)];
				wzLeftSwitch=[CCSprite spriteWithFile:@"Set-music_Word_Cn1.png" rect:CGRectMake(58, 28, 20, 14)];
				wzRightSwitch=[CCSprite spriteWithFile:@"Set-music_Word_Cn1.png" rect:CGRectMake(58, 28, 20, 14)];
				break;
			case 3:
				wzLeft=[CCSprite spriteWithFile:@"Set-music_Word_Cn2.png" rect:CGRectMake(0, 14, 31, 14)];
				wzRight=[CCSprite spriteWithFile:@"Set-music_Word_Cn2.png" rect:CGRectMake(32, 14, 56, 14)];
				wzLeftSwitch=[CCSprite spriteWithFile:@"Set-music_Word_Cn2.png" rect:CGRectMake(58, 28, 20, 14)];
				wzRightSwitch=[CCSprite spriteWithFile:@"Set-music_Word_Cn2.png" rect:CGRectMake(58, 28, 20, 14)];
				break;
			default:
				wzLeft=[CCSprite spriteWithFile:@"Set-music_Word_En.png" rect:CGRectMake(0, 14, 31, 14)];
				wzRight=[CCSprite spriteWithFile:@"Set-music_Word_En.png" rect:CGRectMake(32, 14, 56, 14)];
				wzLeftSwitch=[CCSprite spriteWithFile:@"Set-music_Word_En.png" rect:CGRectMake(58, 28, 20, 14)];
				wzRightSwitch=[CCSprite spriteWithFile:@"Set-music_Word_En.png" rect:CGRectMake(58, 28, 20, 14)];
				break;
		}
		
		wzLeft.anchorPoint=ccp(0,0);
		wzLeft.position=ccp(38,23);
		
		wzRight.anchorPoint=ccp(1,0);
		wzRight.position=ccp(420,23);
		
		wzLeftSwitch.anchorPoint=ccp(0,0);
		wzLeftSwitch.position=ccp(80,23);
		
		wzRightSwitch.anchorPoint=ccp(1,0);
		wzRightSwitch.position=ccp(446,23);
		
		[self addChild:wzLeft z:4];
		[self addChild:wzRight z:4];
		[self addChild:wzLeftSwitch z:4];
		[self addChild:wzRightSwitch z:4];
		
		
				
	}
	return self;
}

-(void)LeftPressed{
	[wzLeftSwitch setTextureRect:CGRectMake(78, 28, 20, 14)];
		
}

-(void)RightPressed{
	[wzRightSwitch setTextureRect:CGRectMake(78, 28, 20, 14)];
	
}

-(void)DownPressed{
	
	
}




//取屏幕相应区域调用函数，改变选项背景框
-(void) registerWithTouchDispatcher
{
	[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:NO];
}

-(BOOL) ccTouchBegan:(UITouch*)touch withEvent:(UIEvent*)event
{
	
	
	CGPoint location = [touch locationInView: [touch view]];
	CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:location];
	
	//音乐
	CGRect MusicRect = CGRectMake(bgLeft.position.x, bgLeft.position.y, bgLeft.contentSize.width, bgLeft.contentSize.height);
	if (CGRectContainsPoint(MusicRect, convertedLocation)) {
		
		[bgLeft setTextureRect:CGRectMake(0, 22, 104, 22)];
	}
	
	//音效
	CGRect SoundRect = CGRectMake(bgRight.position.x-bgRight.contentSize.width, bgRight.position.y, bgRight.contentSize.width, bgRight.contentSize.height);
	if (CGRectContainsPoint(SoundRect, convertedLocation)) {
		[bgRight setTextureRect:CGRectMake(0, 22, 104, 22)];
	}	
	
	//继续
	CGRect ContinueRect = CGRectMake(bgDown.position.x-bgDown.contentSize.width/2, bgDown.position.y-bgDown.contentSize.height/2, bgDown.contentSize.width, bgDown.contentSize.height);
	if (CGRectContainsPoint(ContinueRect, convertedLocation)) {
		[bgDown setTextureRect:CGRectMake(0, 28, 216, 28)];
	}		
	
	
	return YES;
}



- (void)ccTouchEnded:(UITouch*)touch withEvent:(UIEvent *)event
{
	CGPoint location = [touch locationInView: [touch view]];
	CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:location];
	
	//音乐
	CGRect MusicRect = CGRectMake(bgLeft.position.x, bgLeft.position.y, bgLeft.contentSize.width, bgLeft.contentSize.height);
	if (CGRectContainsPoint(MusicRect, convertedLocation)) {
		
		[bgLeft setTextureRect:CGRectMake(0, 0, 104, 22)];
		[self LeftPressed];
		
		
	}
	
	//音效
	CGRect SoundRect = CGRectMake(bgRight.position.x-bgRight.contentSize.width, bgRight.position.y, bgRight.contentSize.width, bgRight.contentSize.height);
	if (CGRectContainsPoint(SoundRect, convertedLocation)) {
		[bgRight setTextureRect:CGRectMake(0, 0, 104, 22)];
		[self RightPressed];
		
	}	
	
	//继续
	CGRect ContinueRect = CGRectMake(bgDown.position.x-bgDown.contentSize.width/2, bgDown.position.y-bgDown.contentSize.height/2, bgDown.contentSize.width, bgDown.contentSize.height);
	if (CGRectContainsPoint(ContinueRect, convertedLocation)) {
		[bgDown setTextureRect:CGRectMake(0, 0, 216, 28)];
	}	

}	







@end
